package com.github.niefy.modules.third.controller;

import com.github.niefy.common.utils.R;
import com.github.niefy.modules.third.form.ThirdJsapiForm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * JS-SDK及微信开放标签服务接口（可归属wx模块）
 */
@RestController
@RequestMapping("/third/jsapi")
@Tag(name = "JS-SDK及微信开放标签服务接口")
@RequiredArgsConstructor
@Slf4j
public class ThirdJsapiController {

    private final WxMpService wxMpService;


    /*
    *提供前端调用jsapi时所需要的签名等信息
    * */
    @PostMapping("/createJsapiSignature")
    @Operation(summary = "提供前端调用jsapi时所需要的签名等信息",description = "")
    public R createJsapiSignature(HttpServletRequest request, HttpServletResponse response,
                                  @Validated @RequestBody ThirdJsapiForm form) throws WxErrorException {
        this.wxMpService.switchoverTo((String) request.getAttribute("appId"));
        var jsapiSignature = wxMpService.createJsapiSignature(form.getUrl());
        return R.ok().put(jsapiSignature);
    }
}
